package com.gxc.datacore.ws.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gxc.datacore.ws.domain.RequestInfo;
import com.gxc.dbcore.dao.SqlDao;
import com.gxc.dbcore.service.BeanService;
import com.gxc.dbcore.util.CompareItem;
import com.gxc.dbcore.util.CompareType;
import com.gxc.dbcore.util.ConditionUtil;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;

@Service
public class RequestInfoService {

	@Autowired
	private SqlDao sqlDao;
	@Autowired
	private BeanService beanService;

	public RequestInfo retrieve(String id) {
		return beanService.get(sqlDao, RequestInfo.class, id);
	}

	public int create(RequestInfo requestInfo) {
		return beanService.create(sqlDao, requestInfo);
	}

	public int update(RequestInfo requestInfo) {
		return beanService.update(sqlDao, requestInfo);
	}

	public int delete(String id) {
		return beanService.delete(sqlDao, RequestInfo.class, id);
	}

	public int delete(String[] id_array) {
		return beanService.delete(sqlDao, RequestInfo.class, id_array);
	}

	@SuppressWarnings("rawtypes")
	private List<CompareItem> buildConditions(Map params) {
		List<CompareItem> list = new ArrayList<CompareItem>();
		list.add(new CompareItem("user_name", CompareType.LIKE_START));
		list.add(new CompareItem("object_id", CompareType.EQUAL));
		list.add(new CompareItem("request_time", CompareType.BETWEEN));
		list = ConditionUtil.setParamListValue(list, params);
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List list(Map params) {
		List<CompareItem> list = this.buildConditions(params);
		String fieldSort = ConditionUtil.getFieldSort(params);
		return beanService.list(sqlDao, RequestInfo.class, list, fieldSort);
	}

	@SuppressWarnings("rawtypes")
	public PageList listPage(Map params, PageInfo pageInfo) {
		List<CompareItem> list = this.buildConditions(params);
		String fieldSort = ConditionUtil.getFieldSort(params);
		return beanService.listPage(sqlDao, RequestInfo.class, list, fieldSort,
				pageInfo);
	}

}
